package com.thesis.service.impl;

import com.thesis.dao.SysFileMapper;
import com.thesis.entity.SysFile;
import com.thesis.service.FileService;
import com.github.pagehelper.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

/**
 * Created by Anvy at 2020/4/19.
 */
@Service("fileService")
public class FileServiceImpl implements FileService {

    private static  final Logger logger= LoggerFactory.getLogger(FileServiceImpl.class);

    @Value("${com.jane.file.baseFilePath}")
    private String baseFilePath;

    @Autowired
    private SysFileMapper fileMapper;


    @Override
    public int deleteByPrimaryKey(Long id) {
        return fileMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(SysFile record) {
        return fileMapper.insert(record);
    }

    @Override
    public int insertSelective(SysFile record) {
        return fileMapper.insertSelective(record);
    }

    @Override
    public SysFile selectByPrimaryKey(Long id) {
        return fileMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(SysFile record) {
        return fileMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(SysFile record) {
        return fileMapper.updateByPrimaryKey(record);
    }

    @Override
    public Page<SysFile> findPageList(SysFile file) {
        return fileMapper.findPageList(file);
    }

    @Override
    public Page<SysFile> findPageListByVague(SysFile queryParam, String vagueParam) {
        return fileMapper.findPageListByVague(queryParam,vagueParam);
    }

    @Override
    public Integer deleteBatchByIds(List<Integer> idList) {
        return fileMapper.deleteBatchByIds(idList);
    }

    /**
     * 直接保存或者先删除历史数据再保存
     * @param sysFile
     * @param file
     * @return
     */
    @Override
    public SysFile saveOrUpdateFile(SysFile sysFile, MultipartFile file) {
        if(sysFile==null||file==null||file.isEmpty()){
            logger.debug("sysFile||file 对象为空！");
            return null;
        }
        String categoryCode = sysFile.getCategoryCode();
        long objectId=sysFile.getObjectId();
        if(StringUtils.isEmpty(categoryCode)||objectId<1){
            logger.debug("请为sysFile设置分类编码和所属对象ID！");
            return null;
        }
        if (!file.isEmpty()) {
            String originalFilename = file.getOriginalFilename();
            String extendName = originalFilename.substring(originalFilename.lastIndexOf("."));
            long size = file.getSize();
            logger.debug("file.getResource:"+file.getResource());
            String saveFileName= UUID.randomUUID().toString().replaceAll("-","")+extendName;
            File saveFile=new File(baseFilePath+File.separator+saveFileName);
            if(!saveFile.getParentFile().exists()){
                saveFile.getParentFile().mkdirs();
            }
            try {
                //保存文件到最终路径
                file.transferTo(saveFile);
            } catch (IOException e) {
                logger.error(e.getMessage(),e);
            }
            sysFile.setFileName(originalFilename);
            sysFile.setFileType(file.getContentType());
            sysFile.setFileUrl(baseFilePath+File.separator+saveFileName);
            sysFile.setFileSize(size+"");
            sysFile.setSaveName(saveFileName);
            //删除以前数据
            SysFile delFile=new SysFile();
            delFile.setCategoryCode(sysFile.getCategoryCode());
            delFile.setObjectId(sysFile.getObjectId());
            Integer del = fileMapper.deleteBySysFile(delFile);
            fileMapper.insertSelective(sysFile);
        }
        return sysFile;
    }

    @Override
    public Integer deleteBySysFile(SysFile file) {
        return fileMapper.deleteBySysFile(file);
    }

    @Override
    public Integer deleteBatchByMap(HashMap map) {
        return fileMapper.deleteBatchByMap(map);
    }
}
